package com.hangwei.paperless.adapter.sub;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.facebook.drawee.view.SimpleDraweeView;
import com.hangwei.paperless.R;
import com.hangwei.paperless.model.bean.DataFilling;
import com.hangwei.paperless.utils.NotFastClickListener;
import com.hangwei.paperless.view.SealHelper;
import com.twelve.all.adapter.MyAdapter;
import com.twelve.all.net.API;
import com.twelve.all.utils.Tools;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 张龙臣 on 2020/11/28.
 */

public abstract class SubDataAdapter extends MyAdapter<DataFilling, BaseViewHolder> {
    public SubDataAdapter(@Nullable List<DataFilling> data) {
        super(R.layout.item_sub_data, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, final DataFilling item) {
        helper.setIsRecyclable(false);
        String billName = item.billType == 1 ? "零件状态记录单" : (item.billType == 2 ? "衬套装配单" : (item.billType == 3 ? "衬套加工单" : "未知类型"));
        helper.setText(R.id.tvSeq, Tools.checkedString(item.seq))
                .setText(R.id.tvName, billName)
                .setText(R.id.tvRemark, Tools.checkedString(item.remark));
        ImageView ivObliqueLine = helper.getView(R.id.ivObliqueLine);
        RecyclerView recyclerViewPic = helper.getView(R.id.recyclerViewPic);
        ImageView ivSee = helper.getView(R.id.ivSee);
        ImageView ivEdit = helper.getView(R.id.ivEdit);
        TextView tvNaType = helper.getView(R.id.tvNaType);
        ImageView tvNa = helper.getView(R.id.tvNa);
        RelativeLayout flUserSeal = helper.getView(R.id.flUserSeal);
        LinearLayoutManager layout = new LinearLayoutManager(mContext);
        layout.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerViewPic.setLayoutManager(layout);
        recyclerViewPic.setAdapter(new PicAdapter(item.picList));
        if (item.isNA) {
            ivSee.setVisibility(View.VISIBLE);
            helper.itemView.setBackgroundColor(mContext.getResources().getColor(R.color.lightGray));
            ivObliqueLine.setVisibility(View.VISIBLE);
            tvNa.setVisibility(View.GONE);
            ivEdit.setVisibility(View.GONE);
            tvNaType.setVisibility(View.VISIBLE);
            tvNaType.setText(Tools.checkedString(item.naTypeCode));
            SealHelper sealHelper = new SealHelper(mContext) {
                @Override
                protected void onSeal() {
                    //seal(item);不会有
                }
            };
            final boolean able = true;
            sealHelper.bindData(new SealHelper.SealBean(true, item.technicianStampId, item.technicianId, item.technicianStampTypeCode, item.technicianStampCode, item.technicianDate,item.technician, able));
            flUserSeal.addView(sealHelper.getItemView());
        } else {
            if (item.isComplete) {
                helper.itemView.setBackgroundColor(mContext.getResources().getColor(R.color.complete));
            } else
                helper.itemView.setBackgroundColor(mContext.getResources().getColor(R.color.white));
            ivObliqueLine.setVisibility(View.GONE);
            tvNaType.setVisibility(View.GONE);
            flUserSeal.removeAllViews();
            if (item.isComplete) {
                ivSee.setVisibility(View.VISIBLE);
                tvNa.setVisibility(View.GONE);
                ivEdit.setVisibility(View.GONE);
            } else {
                ivSee.setVisibility(View.GONE);
                tvNa.setVisibility(View.VISIBLE);
                ivEdit.setVisibility(View.VISIBLE);
                tvNa.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        seal(item);
                    }
                });
            }
        }
//        helper.itemView.setOnClickListener(new NotFastClickListener() {
//            @Override
//            protected void doubleClick(View v) {
//                onItemDoubleClick(item);
//            }
//        });
        helper.setOnClickListener(R.id.ivEdit, new NotFastClickListener() {
            @Override
            protected void isNotFastClick(View v) {
                onItemDoubleClick(item);
            }
        });
        helper.setOnClickListener(R.id.ivSee, new NotFastClickListener() {
            @Override
            protected void isNotFastClick(View v) {
                onItemDoubleClick(item);
            }
        });
    }

    protected abstract void onItemDoubleClick(DataFilling item);

    protected abstract void seal(DataFilling item);

    private class PicAdapter extends RecyclerView.Adapter {

        private ArrayList<String> mPicList;

        public PicAdapter(ArrayList<String> picList) {
            mPicList = picList;
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            return new RecyclerView.ViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_item_sub_data, viewGroup, false)) {
            };
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
            SimpleDraweeView sdv = viewHolder.itemView.findViewById(R.id.sdv);
            sdv.setImageURI(API.BASE_URL + mPicList.get(i));
            final int position = i;
            sdv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showBigPic(mPicList, position);
                }
            });
        }

        @Override
        public int getItemCount() {
            return mPicList == null ? 0 : mPicList.size();
        }
    }

    protected abstract void showBigPic(ArrayList<String> picList, int position);
}
